package company.permissionsystem.service;

import company.permissionsystem.dao.UserDao;
import company.permissionsystem.model.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public SysUser findUserByUserName(String username) {
        return userDao.findUserByUserName(username);
    }

    public SysUser findUserByUserId(Long id) {
        return userDao.findUserByUserId(id);
    }

    public List<SysUser> findAllUser() {
        return userDao.findAllUser();
    }

    @Transactional(rollbackFor = Exception.class)
    public void changeUserStatusById(Long id) {
        SysUser sysUser = findUserByUserId(id);
        if (sysUser.getStatus() == true ) {
            sysUser.setStatus(false);
        } else {
            sysUser.setStatus(true);
        }
        userDao.changeUserStatusById(sysUser);
    }

}
